<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h2>计算属性vs侦听属性</h2>
      <input type="text" v-model="firstname" /> +
      <input type="text" v-model="lastname" />
      <p>全名：{{ fullname }}</p>

      <hr />

      <p>count: {{ count }}</p>
      <p>doubleCount: {{ doubleCount }}</p>
      <button @click="fn">btn</button>
    </div>

    <script src="../Vue.js"></script>
    <script>
      new Vue({
        el: "#app",
        data: {
          firstname: "zhang",
          lastname: "san",
          // fullname: "zhang san",
          count: 1,
          doubleCount: 2,
        },
        // watch: {
        //   firstname(val) {
        //     this.fullname = val + " " + this.lastname;
        //   },
        //   lastname(val) {
        //     this.fullname = this.firstname + " " + val;
        //   },
        // },
        watch: {
          count(val) {
            setTimeout(() => {
              this.doubleCount = val * 2;
            }, 1000);
          },
        },
        computed: {
          fullname() {
            return this.firstname + " " + this.lastname;
          },
          // 计算属性是不能有异步的，必须同步的直接return
          // doubleCount() {
          //   setTimeout(() => {
          //     return this.count * 2;
          //   }, 1000);
          // },
        },

        methods: {
          fn() {
            this.count++;
          },
        },
      });
    </script>
  </body>
</html>
